unit diario;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Provider, SqlExpr, DB, Grids, DBGrids, RXDBCtrl, DBClient,
  DBLocal, DBLocalS, DBCtrls, dbcgrids, StdCtrls, Mask, ExtCtrls, ToolEdit, DateUtils,
  Buttons, ComCtrls, ToolWin, ImgList, ppStrtch, ppMemo, ppCtrls, ppPrnabl,
  ppClass, ppDB, ppBands, ppCache, ppDBPipe, ppComm, ppRelatv, ppProd,
  ppReport;

type
  TfrmDiario = class(TForm)
    cdsPersona: TSQLClientDataSet;
    cdsPersonaID: TSmallintField;
    cdsPersonaAPENOM: TStringField;
    sPersona: TDataSource;
    Panel1: TPanel;
    Panel2: TPanel;
    RxDBGrid2: TRxDBGrid;
    cdsDiario: TSQLClientDataSet;
    sDiario: TDataSource;
    cdsDiarioID: TIntegerField;
    cdsDiarioPIDE: TSmallintField;
    cdsDiarioFECHA: TDateField;
    Label2: TLabel;
    DBMemo1: TDBMemo;
    cdsDiariopidio: TStringField;
    Pi: TLabel;
    DBLookupComboBox1: TDBLookupComboBox;
    DBCtrlGrid1: TDBCtrlGrid;
    DBText1: TDBText;
    DBMemo2: TDBMemo;
    DBText2: TDBText;
    DBNavigator1: TDBNavigator;
    cdsDiarioIDOBRA: TIntegerField;
    Label1: TLabel;
    DBLookupComboBox2: TDBLookupComboBox;
    cdsDiarioDETALLE: TStringField;
    cdsDiarioobra: TStringField;
    DBText3: TDBText;
    DBDateEdit1: TDBDateEdit;
    cdsDiarioCONCLUSION: TStringField;
    DBEdit1: TDBEdit;
    Label3: TLabel;
    DBText4: TDBText;
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    filtro: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    SpeedButton1: TSpeedButton;
    rb2: TRadioButton;
    rb1: TRadioButton;
    Label6: TLabel;
    filtroAux: TEdit;
    rb3: TRadioButton;
    rb4: TRadioButton;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ImageList1: TImageList;
    ToolButton2: TToolButton;
    reporte: TppReport;
    ppDBPipeline1: TppDBPipeline;
    ppHeaderBand1: TppHeaderBand;
    ppDetailBand1: TppDetailBand;
    ppDBText1: TppDBText;
    ppDBMemo1: TppDBMemo;
    ToolButton3: TToolButton;
    ppShape1: TppShape;
    ppDBText2: TppDBText;
    ppDBText3: TppDBText;
    DBText5: TDBText;
    ppDBText4: TppDBText;
    ToolButton4: TToolButton;
    ppDBText5: TppDBText;
    ToolButton5: TToolButton;
    cdsDiarioID_USUARIO: TSmallintField;
    procedure cdsDiarioAfterPost(DataSet: TDataSet);
    procedure cdsPersonaAfterPost(DataSet: TDataSet);
    procedure RxDBGrid2KeyPress(Sender: TObject; var Key: Char);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cdsDiarioBeforeOpen(DataSet: TDataSet);
    procedure filtroKeyPress(Sender: TObject; var Key: Char);
    procedure SpeedButton1Click(Sender: TObject);
    procedure filtroAuxChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure cdsDiarioNewRecord(DataSet: TDataSet);
  private
    FidUsuario: word;
    procedure SetidUsuario(const Value: word);
    { Private declarations }
  published
    property idUsuario: word read FidUsuario write SetidUsuario;
  public
    constructor CreateNuevo(AOwner: TComponent; usuario: word);
    //constructor Create(AOwner: TComponent);
  end;

var
  frmDiario: TfrmDiario;

implementation

uses MDatos, main, Recursos, buscarProveedor, talonarioOC, cotizador,
  localizarMaterial, pedidosCotizacion;

{$R *.dfm}

{ TfrmDiario }

constructor TfrmDiario.CreateNuevo(AOwner: TComponent; usuario: word);
//constructor TfrmDiario.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  self.idUsuario := usuario;
  self.edtFechaIni.Date := IncMonth(now, -1);
  self.edtFechaFin.Date := now;
  self.cdsPersona.Open;
  self.cdsDiario.Open; 
  self.ShowModal;
end;

procedure TfrmDiario.cdsDiarioAfterPost(DataSet: TDataSet);
begin
  self.cdsDiario.ApplyUpdates(0);
end;

procedure TfrmDiario.cdsPersonaAfterPost(DataSet: TDataSet);
begin
  self.cdsPersona.ApplyUpdates(0);
end;

procedure TfrmDiario.RxDBGrid2KeyPress(Sender: TObject; var Key: Char);
begin
  Key := upCase(Key);
end;

procedure TfrmDiario.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if (self.cdsPersona.State in [dsInsert, dsEdit]) then
    self.cdsPersona.Post;
  if (self.cdsDiario.State in [dsInsert, dsEdit]) then
    self.cdsDiario.Post;
  frmMain.cds.Close;
  Datos.cPresupuesto.Close;
end;

procedure TfrmDiario.cdsDiarioBeforeOpen(DataSet: TDataSet);
begin
  self.cdsDiario.Params[0].AsString := '%'+self.filtro.Text+'%';
  self.cdsDiario.Params[1].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
  self.cdsDiario.Params[2].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
  self.cdsDiario.Params[3].AsInteger := self.idUsuario;
end;

procedure TfrmDiario.filtroKeyPress(Sender: TObject; var Key: Char);
begin
  if (ord(Key) = VK_RETURN) then
    self.SpeedButton1Click(Sender)
  else Key := upCase(Key);
end;

procedure TfrmDiario.SpeedButton1Click(Sender: TObject);
begin
  self.cdsDiario.Close;
  self.cdsDiario.Open;
end;

procedure TfrmDiario.filtroAuxChange(Sender: TObject);
var
  ds : TSQLDataSEt;
  filtro: string;
begin
  if self.rb1.Checked then
    filtroLookUp(self.cdsDiario, self.cdsDiariopidio, self.filtroAux.Text, 'persona', False)
  else if self.rb2.Checked then
  begin
    self.cdsDiario.Filtered := False;
    ds := TSQLDataSet.Create(nil);
    try
      ds.SQLConnection := Datos.Con;
      ds.CommandText := 'SELECT id_presupuesto FROM presupuesto'
      +' WHERE denominacion like ''%'+self.filtroAux.Text+'%''';
      ds.Open;
      filtro := '';
      if not ds.IsEmpty then
        while not ds.Eof do
        begin
          filtro := filtro+'idObra = '+ds.Fields [0].AsString+' or ';
          ds.Next;
        end;
      if (filtro <> '') then
        filtro := copy (filtro, 1, length(filtro)-4);
      self.cdsDiario.Filter := filtro;
      self.cdsDiario.Filtered := True;
    finally
      ds.Free;
    end;
  end 
  else if self.rb3.Checked then
  begin
    self.cdsDiario.Filtered := False;
    self.cdsDiario.Filter := 'DETALLE like ''%'+self.filtroAux.Text+'%''';
    self.cdsDiario.Filtered := True;
  end
  else
  begin
    self.cdsDiario.Filtered := False;
    self.cdsDiario.Filter := 'CONCLUSION like ''%'+self.filtroAux.Text+'%''';
    self.cdsDiario.Filtered := True;
  end;
end;

procedure TfrmDiario.ToolButton1Click(Sender: TObject);
begin
  TfrmBuscarProveedor.CreateNuevo(self);
end;

procedure TfrmDiario.ToolButton2Click(Sender: TObject);
begin
  TfrmTalonarioOC.CreateNuevo(self, 0);
end;

procedure TfrmDiario.ToolButton3Click(Sender: TObject);
begin
  self.reporte.Print;  
end;

procedure TfrmDiario.ToolButton4Click(Sender: TObject);
begin
  TfrmPedidosCotizacion.CreateNuevo(self, self.idUsuario);
end;

procedure TfrmDiario.ToolButton5Click(Sender: TObject);
begin
  TfrmLocalizarMaterial.CreateNuevo(self);
end;

procedure TfrmDiario.SetidUsuario(const Value: word);
begin
  FidUsuario := Value;
end;

procedure TfrmDiario.cdsDiarioNewRecord(DataSet: TDataSet);
begin
  self.cdsDiarioID_USUARIO.AsInteger := self.idUsuario;
end;

end.
